package com.chezhujie.www.controller;


import com.chezhujie.www.domain.Czjrouleoff;
import com.chezhujie.www.service.CzjrouleoffService;
import com.sys.core.page.Pagination;
import com.sys.user.base.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;





@Controller
@RequestMapping("/rouleoff")
public class CzjrouleoffController extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(CzjrouleoffController.class);
	public CzjrouleoffController() {
	}
 
 
 	/**
	 * czj_rouleoff  优惠新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjrouleoffAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("czjrouleoff", new Czjrouleoff());
      setDict(model);
		return "rouleoff/czjrouleoffEdit";
	}

	/**
	 * czj_rouleoff  优惠保存
	 * 
	 * @param czjrouleoff
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjrouleoffSave.do")
	public String save(Czjrouleoff czjrouleoff, 
			@RequestParam(value = "first", required = false)  MultipartFile first,
			HttpServletRequest request, ModelMap model) throws Exception{ 			
		try {
			//路径
			String pathRoot = request.getSession().getServletContext()
					.getRealPath("");
			String path = "";				
			// 第一张头像
			if (!first.isEmpty() && first.getSize()>0) {
				String uuid = UUID.randomUUID().toString().replaceAll("-", "");
				path = "/uploadvoteorder/" + uuid + "." + "jpg";
				File file = new File(pathRoot + "/uploadvoteorder");
				if (!file.exists()) {
					file.mkdir();
				}
				first.transferTo(new File(pathRoot + path));					
				//更新才删
				String jpgPic = czjrouleoff.getImgPath();					
				if(jpgPic != null && jpgPic.length()>5){
					jpgPic = jpgPic.substring(1, jpgPic.length());
				}
				deleteFile(pathRoot + jpgPic);
				//更新才设置图片路径				
				czjrouleoff.setImgPath(path);
			}
			if(czjrouleoff.getRouleoffId()!=null&&!czjrouleoff.getRouleoffId().equals("")){
				// 编辑								
				czjrouleoffService.updateCzjrouleoff(czjrouleoff);
				model.addAttribute("message",2);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "编辑czj_rouleoff  优惠", "rouleoffId="+czjrouleoff.getrouleoffId()));
			} else {
				// 新增
				czjrouleoff.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				czjrouleoff.setExt1("0");
				czjrouleoffService.insertCzjrouleoff(czjrouleoff);
				model.addAttribute("message",2);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "新增czj_rouleoff  优惠", "rouleoffId="+czjrouleoff.getrouleoffId()));
			}
   		} catch (Exception e) {
   			log.error("czjrouleoff save error:", e); 
            throw e;
   		}
      
		return "redirect:czjrouleoffEdit.do?rouleoffId="+czjrouleoff.getRouleoffId();
	}

	
	/**
	 * czj_rouleoff  优惠保存
	 * 
	 * @param czjrouleoff
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjrouleoffSaveExt1.do")
	public String saveExt1(Czjrouleoff czjrouleoff, 
			HttpServletRequest request, ModelMap model) throws Exception{    
		try {
			if(czjrouleoff.getRouleoffId()!=null&&!czjrouleoff.getRouleoffId().equals("")){
				// 编辑				
				czjrouleoffService.updateCzjrouleoff(czjrouleoff);
				 
				String  message =null;
				
				if("1".equals(czjrouleoff.getExt1())){	
					message = "1";
				}
				if("0".equals(czjrouleoff.getExt1())){	
					message = "0";
				}				
				model.addAttribute("message",message);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "编辑czj_rouleoff  优惠", "rouleoffId="+czjrouleoff.getrouleoffId()));
			} else {
				// 新增
/*				czjrouleoff.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				czjrouleoffService.insertCzjrouleoff(czjrouleoff);*/
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "新增czj_rouleoff  优惠", "rouleoffId="+czjrouleoff.getrouleoffId()));
			}
   		} catch (Exception e) {
   			log.error("czjrouleoff save error:", e); 
            throw e;
   		}
      
		return "redirect:czjrouleoffEdit.do?rouleoffId="+czjrouleoff.getRouleoffId();
	}	
	
	/**
	 * czj_rouleoff  优惠删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("czjrouleoffDelete.do")
	public String delete(@RequestParam("ids") List<String> rouleoffIdList) throws Exception{
		try {
         if (rouleoffIdList != null) {
   			czjrouleoffService.deleteCzjrouleoff(rouleoffIdList);
   		}
         } catch (Exception e) {
   			log.error("czjrouleoff delete error:", e); 
            throw e;
   		}
		return "redirect:czjrouleoffList.do";
	}
	 
	/**
	 * czj_rouleoff  优惠编辑页面
	 * 
	 * @param rouleoffId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjrouleoffEdit.do")
	public String edit(String rouleoffId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
          String message = request.getParameter("message");                  
          request.setAttribute("message", message);
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("rouleoffId", rouleoffId);
         Czjrouleoff czjrouleoff = czjrouleoffService.getCzjrouleoff(param);
   		model.addAttribute("czjrouleoff", czjrouleoff);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjrouleoff edit error:", e); 
            throw e;
   		}
		return "rouleoff/czjrouleoffEdit";
	}

	/**
	 * czj_rouleoff  优惠详情
	 * 
	 * @param rouleoffId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjrouleoffDetail.do")
	public String detail(String rouleoffId, HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception{
      try {
      		Map<String, Object> param = new HashMap<String, Object>();
      		param.put("rouleoffId", rouleoffId);
            Czjrouleoff czjrouleoff = czjrouleoffService.getCzjrouleoff(param);
      		model.addAttribute("czjrouleoff", czjrouleoff);
            setDict(model);
            } catch (Exception e) {
   			log.error("czjrouleoff detail error:", e); 
            throw e;
   		  }
		return "rouleoff/czjrouleoffView";
	}
	
	
	/**
	 * czj_rouleoff  优惠详情
	 * 
	 * @param rouleoffId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("rouleoffQueryImgOrder.do")
	public void detail1111(String imgOrder, String rouleoffId, HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception{
      try {
      		Map<String, Object> param = new HashMap<String, Object>();
      		param.put("imgOrder", imgOrder);     		    		    		
            Czjrouleoff czjrouleoff = czjrouleoffService.queryImgOrder(param);
                          
            //插入时
      		if("".equals(rouleoffId)){  
      			     if(czjrouleoff !=null){
      			    	jsonResult(1,"","重复的顺序",0,response);  
      			     }else{
      			    	jsonResult(1,"","可以添加",1,response);  
      			     }      				      			         
	      		}
      		//更新时      		
      		if(!"".equals(rouleoffId)){
          		//查询自己
          		Map<String, Object> paramMy = new HashMap<String, Object>();
          		paramMy.put("rouleoffId", rouleoffId);
                Czjrouleoff myorder = czjrouleoffService.getCzjrouleoff(paramMy);
                String myordernum = myorder.getImgOrder();
                //和自己相同的可以更新
                if(myordernum.equals(imgOrder)){
                	jsonResult(1,"","可以添加",1,response); 
                }else{
                  //如果跟自己的不相同
                	List<String> listString = czjrouleoffService.getCzjrouleoffALL();
                	System.out.println(listString);
                	boolean flag = true;
                	//循环数据库字段,如果相同,就代表有重复
                	for(String str : listString){
                		if(imgOrder.equals(str)){
                			flag= false;
                		}
                	}
                	System.out.println(flag);
                	if(!flag){
                		jsonResult(1,"","重复的顺序",0,response); 
                	}else{
                		jsonResult(1,"","可以添加",1,response); 
                	}
                	
                	
                }
                
                
                
      		}        		
      		
      		
      		
            } catch (Exception e) {
            jsonResult(1,"","系统异常","",response);
   			log.error("czjrouleoff detail error:", e); 
            throw e;
   		  }
		
	}	
	
	 
	/**
	 * czj_rouleoff  优惠查询（分页）
	 * 
	 * @param czjrouleoff
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjrouleoffList.do")
	public String listPage(Czjrouleoff czjrouleoff, Integer pageNo, String order,
			String sort,HttpServletRequest request, ModelMap model) throws Exception{
      try {   
   		if (pageNo == null)
   			pageNo = 1;
         Pagination pagination = czjrouleoffService.getCzjrouleoffList(toMap(czjrouleoff), 10, pageNo,
   				order, sort);          
   		model.addAttribute("pagination", pagination);
         setDict(model);
         } catch (Exception e) {
			log.error("czjrouleoff listPage error:", e); 
         throw e;
		   }
		return "rouleoff/czjrouleoffList";
	}
	 
	/**
	 * czj_rouleoff  优惠查询
	 * 
	 * @param czjrouleoff
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjrouleoffList1.do")
	public String list(Czjrouleoff czjrouleoff, HttpServletRequest request, ModelMap model) throws Exception{
      try {
   		Map<String,Object> map = czjrouleoffService.getCzjrouleoffList(toMap(czjrouleoff));
   		model.addAttribute("list", map.get("rows"));
   		model.addAttribute("total", map.get("total"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjrouleoff list error:", e); 
            throw e;
   		}
		return "rouleoff/czjrouleoffList";
	}
	 

 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param czjrouleoff
	 * @return
	 */	private Map<String,Object> toMap(Czjrouleoff czjrouleoff){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(czjrouleoff.getRouleoffId()!=null&&!czjrouleoff.getRouleoffId().equals("")){
			map.put("rouleoffId", czjrouleoff.getRouleoffId());
		}
		if(czjrouleoff.getImgOrder()!=null&&!czjrouleoff.getImgOrder().equals("")){
			map.put("imgOrder", czjrouleoff.getImgOrder());
		}
		if(czjrouleoff.getImgPath()!=null&&!czjrouleoff.getImgPath().equals("")){
			map.put("imgPath", czjrouleoff.getImgPath());
		}
		if(czjrouleoff.getImgUrl()!=null&&!czjrouleoff.getImgUrl().equals("")){
			map.put("imgUrl", czjrouleoff.getImgUrl());
		}
		if(czjrouleoff.getRemark()!=null&&!czjrouleoff.getRemark().equals("")){
			map.put("remark", czjrouleoff.getRemark());
		}
		if(czjrouleoff.getCreatetime()!=null&&!czjrouleoff.getCreatetime().equals("")){
			map.put("createtime", czjrouleoff.getCreatetime());
		}
		if(czjrouleoff.getExt1()!=null&&!czjrouleoff.getExt1().equals("")){
			map.put("ext1", czjrouleoff.getExt1());
		}
		if(czjrouleoff.getExt2()!=null&&!czjrouleoff.getExt2().equals("")){
			map.put("ext2", czjrouleoff.getExt2());
		}
		if(czjrouleoff.getExt3()!=null&&!czjrouleoff.getExt3().equals("")){
			map.put("ext3", czjrouleoff.getExt3());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		
	}
 
	//删除更新的图片
	 public void deleteFile(String jgpName){	 
		 File f = new File(jgpName); // 输入要删除的文件位置
		 if(f.exists()){
			 f.delete();
		 }
		
	 }	
     
	@Resource
	private CzjrouleoffService czjrouleoffService;


}